package com.xyd.Servlet;

import com.xyd.Biz.CommodityBiz;
import com.xyd.Biz.FatherOrderBiz;
import com.xyd.Biz.Impl.CommodityBizImpl;
import com.xyd.Biz.Impl.FatherOrderBizImpl;
import com.xyd.Biz.Impl.OrderFormBizImpl;
import com.xyd.Biz.OrderFormBiz;
import com.xyd.Entity.*;
import com.xyd.Util.SnowFlakeIdWorker;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * @ClassName: BuyServlet
 * @Description:
 * @Author 余全盛
 * @Date: 2021/10/21 16:27
 */
@WebServlet("/html/BuyServlet")
public class BuyServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session=req.getSession();
        OrderFormBiz orderFormBiz=new OrderFormBizImpl();
        int productId=Integer.parseInt(req.getParameter("productid")==null||"".equals(req.getParameter("productid"))?"0":req.getParameter("productid"));
       double price=Double.parseDouble(req.getParameter("price")==null||"".equals(req.getParameter("price"))?"0.00":req.getParameter("price"));
        SnowFlakeIdWorker snowFlakeIdWorker = new SnowFlakeIdWorker(2, 2);
        long fatherOrderID = snowFlakeIdWorker.nextId();
        orderFormBiz.addSonOrder(productId, 1, price, price, fatherOrderID);
        User user=(User) session.getAttribute("user");
        List<OrderForm> sonOrderList = orderFormBiz.selectSonOrder(fatherOrderID);
//        生成父订单，跳转支付详情页面
        FatherOrderBiz fatherOrderBiz=new FatherOrderBizImpl();
        fatherOrderBiz.addOrder(fatherOrderID, price, user);
        FatherOrder fatherOrder = fatherOrderBiz.selectFatherOrderByID(fatherOrderID);
        CommodityBiz commodityBiz=new CommodityBizImpl();
        List<Commodity> commodityList=commodityBiz.getAllCommodity();
        session.setAttribute("sonOrderList", sonOrderList);
        session.setAttribute("fatherOrder", fatherOrder);
        session.setAttribute("commodityList", commodityList);
    }
}
